INDEX

SYMBOLS

+ (addition operator), 27

&& (AND operator), 38

= (assignment operator), 19

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SYMBOLS

+ (addition operator), 27

&& (AND operator), 38

= (assignment operator), 19

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

+ (addition operator), 27

&& (AND operator), 38

= (assignment operator), 19

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

&& (AND operator), 38

= (assignment operator), 19

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

= (assignment operator), 19

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

... (closed range operator), 48

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

/ (division operator), 27

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

== (equal to operator), 36

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

> (greater than operator), 37

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

< (less than operator), 37

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

% (modulo operator), 30 , 55 , 105

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

* (multiplication operator), 27

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

?? (nil coalescing operator), 65 , 151 , 185

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

!= (not equal to operator), 36

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

! (NOT operator), 38

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

|| (OR operator), 38

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

| (pipe), 248

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

- (subtraction operator), 27

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

A

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

addition operator (+ ), 27

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

alpha (transparency setting), 286–287

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

anchorPoint property, 208–209 , 287

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

AND operator (&& ), 38

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

app icon, adding to project, 125

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Apple ID (Apple account)

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 5

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

for running app on a device, 14

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

signing in to Xcode, 7

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

applications. See apps

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

application delegate, 174

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

managing app lifecycle, 177

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

shared property, 178

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

apps, 3. See also BirthdayTracker; Hello World; Schoolhouse Skateboarder

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 5–9 , 122–125 , 205–206

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

lifecycle, 174 , 177

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arc4random_uniform() function, 229

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arguments, 82

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in a function call, 83

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

labels, 84 , 88 , 89

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

custom, 90

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 90–91

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arithmetic operators, 27–30

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arrays

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

accessing items, 69

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding items, 70

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

index, 67

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Index out of range error, 69–70

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

initializing, 68

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

looping through, 50 , 74–75

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

mutable and immutable, 68

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties, 73

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing items, 71–72

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

replacing items, 72

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

artwork. See images

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

asset catalogs, 125 , 206–207 , 214

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

assignment operator (= ), 19

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Attributes Inspector, 12 , 127

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

auto layout, 138–139

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

B

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

behavior, of an object, 96

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

bitmasks, 247–249

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

BirthdayTracker (app), 121 , 122

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

AddBirthdayViewController class, 144 , 145

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

alphabetizing birthdays, 185–186

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

app icon, adding, 125

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Birthday class, 142–144 , 175–176

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

BirthdaysTableViewController class, 127–129 , 156–158

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating project, 122–125

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying birthdays

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

connecting user input to display, 166–172

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating list of birthdays, 155–166

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

display name, adding, 126

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

notifications

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

permissions for, 192–194

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 199

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scheduling, 194–199

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing birthdays, 186–189

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

saving birthdays, 173–181

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

user input

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Add button, 130–133

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

auto layout, 138–139

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Cancel button, 139 , 153–154

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

controllers, 127–130 , 145–146

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

date picker, 137–138 , 147–150 , 152

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

input controls, 133 , 146–147

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Save button, 139 , 150–151 , 153

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

text fields, 136–137 , 147–149 , 151–152

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

user interface labels, 134–136

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

body, of a function, 83

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Bool data type, 24

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Boolean

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

expressions, 36–40

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

values, 24

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

break keyword, 105–106

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Bundle Identifier, 9

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

bundles, 294

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

C

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Calendar class, 195–197

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

calling a function, 83

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

cases, of enums, 259–260 , 280–281

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

casting, 26. See also downcasting

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGFloat data type, 210 , 219

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGPoint struct, 210

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGRect struct, 210

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGVector data type, 240

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

classes, 95

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

accessing properties, 98–100

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 96

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

definition, writing, 96–97

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

inheritance, 108–110

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

initializers, 98 , 100–103

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

instances, 96

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

methods, 96 , 98 , 103–106

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

naming, 96–97

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

objects, 95

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties, 97 , 219

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

self keyword, 106–107

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

superclasses and subclasses, 108–110

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

class keyword, 96

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

closed range operator (... ), 48

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

code blocks

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nesting, 54–55

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scope, 55–57

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

collections, looping through, 50

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

comments, 32–33

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

comparison operators, 36–38

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

compound assignment operators, 31–32

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

compound Boolean expressions, 38–40

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

conditional statements, 36 , 40–45

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

console. See debug area

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

constants

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

declaring and creating, 20

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

naming, 22

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

when to use, 21

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

coordinates, 11 , 210

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Core Data framework, 174

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding to project, 123 , 174

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

deleting managed objects, 188

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

entities and attributes, 174–176

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

fetching managed objects, 181–182

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

managed object context, 178

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

saving managed objects, 180

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sortDescriptors property, 185–186

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

count property

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

array, 73

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dictionary, 78

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating a new project, 6 , 122–125 , 205–206

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

D

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

databases, 173–174 . See also Core Data framework

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

data types, 22

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Bool , 24

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

casting, 26

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGFloat , 210 , 219

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGVector , 240

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

custom class, 95

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

declaration of, 23

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Double , 24

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

downcasting, 113–114

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Float , 24

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Int , 24

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

String , 24–25

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

TimeInterval , 232 , 272

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

type inference, 25–26

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UInt32 , 247

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Date

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

class, 144

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

data type, 159

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

DateComponents class, 195–197

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

date formatter, 159

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

date picker

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 137–138

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

getting date from, 152

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

setting a maximum date, 149–150

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

debug area, 10

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying output, 83

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

opening, 19 , 48

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

decimal numbers, 24

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

definition, writing for a class, 96–97

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

degrees, of rotation, 255

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

delegation, 166–168

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

delegate, creating, 170–171

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

protocols

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adopting, 168–170

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 168

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

device orientation, 124 , 211–213

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

devices, running apps on, 14–16

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dictionaries, 75

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

accessing values, 76–77

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding items, 77

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

initializing, 75–76

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

looping through, 78–79

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

mutable and immutable, 75

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

optionals, 76

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties, 78

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing items, 77

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

replacing items, 77–78

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dismissing view controller, 153–154

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

division operator (/ ), 27

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dot notation, 98 , 104

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

do-try-catch blocks, handling errors with, 180

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Double data type, 24

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

downcasting, 113–114

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

E

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

else if statements, 41–43 . See also if statements

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

else statements, 41 . See also if statements

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

enums (enumerations), 259–260 , 280–281

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

equal to operator (== ), 36

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

errors

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

fixing with Xcode, 99–100

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

handling with do-try-catch blocks, 180

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Index out of range error, 69–70

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playground, 21

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

exhaustive switch statements, 105

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

F

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Float data type, 24

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

fonts

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in SpriteKit, 270

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in storyboard, 12

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

for-in loops

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arrays, 50 , 74–75

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

collections, 50

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dictionaries, 78–79

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

ranges, 48–49

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

formatting strings, 273–274

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

frames, 210

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

func keyword, 82

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

functions, 81

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

argument labels, 84 , 88 , 89–91

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arguments, 82

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

body, 83

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

calling, 83

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

input parameters, 82 , 84–88

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

naming, 82

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

return values, 91–93

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

void, 82

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

G

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

game engine. See SpriteKit

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

game loop update(_:) method, 231–233

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

GameScene class, 208

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

gesture recognizers, 234–235

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

greater than operator (> ), 37

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

graphics. See images

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

H

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Hello World (app)

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding a label, 10–12

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating and naming, 6–9

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running in simulator, 12–13

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running on a device, 14–16

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

stopping, 13

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

I

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

IBAction, 150

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

IBOutlet

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

connecting to storyboard, 147–149

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

setting up, 146–147

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

if-let statements, 62–64

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

if statements, 40–43

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

images

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding to project, 206–207

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

finding, 204

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sizing for devices, 213–214

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

suffixes, 214

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

implicitly unwrapped optionals, 64

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

index, 67. See also arrays

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

IndexPath struct, 165–166

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

inheritance, in a class, 108–110

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

initializers, for classes, 98

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

calling with input parameters, 102–103

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

with input parameters, 101–102

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

without input parameters, 100–101

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

default, 100

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

init keyword, 100–102

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

input parameters, of a function, 82 , 84–88

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

installing Xcode, 4–5

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

instances, of a class, 96

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Int data type, 24

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

integers, 23 , 24

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

iOS, 4

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

iPhone simulator. See simulator

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

isEmpty property

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

array, 73

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dictionary, 78

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

is keyword, 110

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

iteration, 49

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

K

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

keys, 75. See also dictionaries

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

L

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

labels

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding to storyboard, 10–11

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

positioning, 134–136

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in SpriteKit, 268–272 , 273–274

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

landscape vs. portrait, device orientation, 124 , 211–213

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

less than operator (< ), 37

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

let keyword, 20

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

local notifications. See notifications

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

logical operators, 38–40

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

long-press gestures, 234

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

loops, 47

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

for-in loops. See for-in loops

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nesting, 54–55

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

while loops, 50–53

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

M

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

managed object context, 178

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

MARK: keyword, for marking file sections, 170

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

methods, 96

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

calling, 104

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 103

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

helper methods, 104–106

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

initializers, 98

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

selectors, 235

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

mobile apps, 4

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

modal view controller, 132

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

modulo operator (% ), 30 , 55 , 105

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

modulus. See modulo operator

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

multiplication operator (* ), 27

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

N

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

naming

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

classes, 96–97

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

constants and variables, 22

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

functions, 82

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

navigation controller, 129

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

accessing in code, 171–172

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding buttons, 130–133 , 139

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding a title, 130

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Navigator pane, 9–10 , 124

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nesting code blocks, 54–55

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nil coalescing operator (?? ), 65 , 151 , 185

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nil value, 60

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

not equal to operator (!= ), 36

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

notifications, 191

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

changing settings, 194

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 199

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

requesting authorization, 192–194

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scheduling, 194–199

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

trigger, 195–196

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

NOT operator (! ), 38

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

NSKeyedUnarchiver , 294

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

O

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Objective-C, 4

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Object Library, 10

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

objects, 95–96

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

opening a project, 13

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

operands, 27

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

operators

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

arithmetic, 27–30

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

comparison, 36–38

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

compound assignment, 31–32

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

logical, 38–40

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

modulo (% ), 30 , 55 , 105

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

order of operations, 30–31

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

optionals, 59 , 60

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

declaring, 60

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dictionary values, 76

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

if-let statements, 62–64

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nil coalescing operator (?? ), 65 , 151

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

optional binding, 62–64

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

unwrapping

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

forced, 61–62

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

implicit, 64

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

order of operations, 30–31

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

OR operator (|| ), 38

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

P

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

pan gestures, 234

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

parameters, of a function, 82 , 84–88

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

parentheses

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

after function names, 83

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

for order of operations, 30–31 , 39

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

particle emitters, 291–297

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics bodies, 240

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

applying forces to, 250–251

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

shapes, 241–242

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics categories, 247

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics engine. See SpriteKit physics engine

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

pinch gestures, 234

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

pipe (| ), 248

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playground

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

debug area, 19–21

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

opening, 17–18

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

results sidebar, 18–19

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

PNG (Portable Network Graphics) files, 207

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

portrait vs. landscape, device orientation, 124 , 211–213

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

positioning

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sprites, 210 , 220–221 , 226–227 , 263

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

storyboard elements, 134–136

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

prepare(for:sender:) method, 171–172

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

print(_:) function, 19 , 82

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

projects. See also individual projects

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 6–9 , 122–125 , 205–206

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

opening, 13

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running on device, 14–16

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running in simulator, 12–13

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

saving, 13

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

of arrays, 73

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

of classes, 96–97

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

accessing, 98–100

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

default values, 97

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

of dictionaries, 78

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

protocol, for delegation, 167 , 168–170

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

R

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

radians, of rotation, 255

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

random numbers, 229

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

ranges, looping through, 48–49

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

raw values, of enums, 259–260

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

rectangles, for frames, 210

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

refactoring code, 182

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

reference types, 115–116

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

repeat-while loops, 52–53

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

return keyword, 91

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

return values, 91–93

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

rotating device, in simulator, 213

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running an app

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in simulator, 12–13

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

on a device, 14–16

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

S

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

saving

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

birthdays, in BirthdayTracker, 173–181

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

managed objects, in database, 180

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

projects, 13

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scene

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in SpriteKit, 205 , 208

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in a storyboard, 10

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Schoolhouse Skateboarder (app), 203

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

anchorPoint , setting for scene, 208–209

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

bricks

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 224–225

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

animating, 226–230

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

leaving gaps, 229–230

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics bodies for, 245–246

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

contacts and collisions, 246–250

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating the project, 205–206

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

ending the game, 254–255

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

game loop, 231–233

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

game state, 280–281

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

gems

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

collecting, 267–268

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 264–265

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

spawning, 262–264

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

updating, 265–267

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

gravity, simulating, 235–238 , 240

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

images

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying background, 207–211

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

downloading, 204 , 206

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

filling the screen, 215

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

menus (MenuLayer class), 282

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying, 286–288

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

message label, 283–285

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 288–289

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

score label, 285–286

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

orientation, setting, 211–213

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

platforms, adding multilevel, 258–262

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scores

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying with labels, 268–272

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

for collecting gems, 275–276

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

high scores, 276

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

updating, 272–275

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

skater

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

checking rotation of, 255

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

jumping, 234–238 , 250–251

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics body for, 244–245

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

resetting, 220–221

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Skater class

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 218

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

instantiating, 219

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sounds

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding to project, 289–290

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

downloading, 204 , 206

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playing, 290

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sparks, 291–297

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

speeding up the game, 257–258

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

starting the game, 251–254

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scope, 55–57 , 103–104

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SDK (Software Development Kit), 4

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

segues, 132 , 171–172

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

selectors, 235

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

self keyword, 106–107

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

simulator (iPhone simulator)

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

rotating device, 213

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

running app on, 12–13

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Size Inspector, 11 , 127

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKAction object, 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKEmitterNode , 294–295

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKLabelNode class, 269–273

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKPhysicsWorld class, 240

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

.sks (SpriteKit scene) files, 206 , 291 , 294

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKScene class, 208 , 240

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKSpriteNode class

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating, 210 , 218 , 225

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

subclassing, 218 , 282

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Software Development Kit (SDK), 4

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sound effects

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding to project, 289–290

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

finding, 204

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playing, in SpriteKit, 290

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sparks, adding with particle emitters, 291–297

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

spawning, in games, 225

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SpriteKit, 204

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

animating via actions, 284–285 , 295

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

debug information, 223

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

game loop update(_:) method, 231–233

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

didMove(to:) method, 208 , 210 , 234 , 253

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

filling the screen, 215

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

nodes, 223

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

getting by name, 273

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

name property of, 271 , 273

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics engine. See SpriteKit physics engine

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

scene size, 215

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKAction , 284–285 , 295

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKEmitterNode , 294–295

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKLabelNode , 269–273

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

.sks (SpriteKit scene) files, 206

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKScene class, 208

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKSpriteNode class, 210

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

subclassing, 218 , 282

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sound effects, 289–290

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

spawning, 225

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sprites. See sprites

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SpriteKit physics engine, 239

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

applying forces, 250–251

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

categoryBitMask property, 248

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

collisionBitMask property, 248

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

contacts and collisions, 246–250 , 267

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

contactTestBitMask property, 248–249

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

didBegin(_:) method, 249–250 , 267

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

gravity, 240

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics bodies, 240

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

applying to sprite, 244–245 , 263

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

properties, 242–244 , 253–254

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

shapes, 241–242

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

physics categories, 247

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKPhysicsBody class, 241–251

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKPhysicsContactDelegate protocol, 249–250

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

SKPhysicsWorld class, 240

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

speed, 240

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

vectors, 240 , 250–251

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sprites, 204

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 210–211 , 220 , 222 , 225 , 263

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

animating, 226–229 , 231–233 , 284–285

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

children and parents, 211 , 220

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

color, 286–287

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying, 210 , 220 , 263

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

moving, 284–285

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

overlapping, 221

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

positioning, 210 , 220–221 , 226–227 , 263

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

removing, 226–227 , 252 , 264–265 , 288

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

zPosition property, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

state, of an object, 95–96

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

storyboard, 9 , 126

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding a navigation controller, 129

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding view controllers, 127

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

auto layout, 138–139

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

elements

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 10

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

positioning, 11–12 , 134–136

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

segues, 132

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

String data type, 24

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

strings, 24

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

concatenation, 25

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

embedded variables, 49

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

formatting, 273–274

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

structs, 117–118

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

CGPoint , 210

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

IndexPath , 165–166

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

use in Schoolhouse Skateboarder, 247

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

subclasses, 108–110 , 218

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

subtraction operator (- ), 27

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

superclasses, 108–110

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

super keyword, 109

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Swift, 4

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating file, 142–143

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playground. See playground

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

safe language, 59

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

swipe gestures, 234

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

switch keyword, 44

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

switch statements, 44–45 , 105

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

T

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

table view, 155

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

cells

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 158–159

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

displaying, 164–166

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

data source, 162

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

IndexPath struct, 165

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

reloadData() method, 169

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

rows, 162–163

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

deleting, 187–188

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

editing, 187

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

sections, 162–163

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

table view controller, adding to storyboard, 127

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

tap gestures, 234–235

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

targets, for selectors, 235

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

text alignment

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in SpriteKit, 269 , 271 , 283 , 286

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in storyboard, 12

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

text fields

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

adding, 136

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

reading text from, 151–152

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

textures, for physics bodies, 241–242 , 244–245

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

TimeInterval data type, 231–233 , 272

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

transparency setting (alpha), 286–287

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

typecasting, 110–113

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

type inference, 25

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

U

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UIDatePicker class, 146 , 149 . See also date picker

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UInt32 data type, 247

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UITableViewController class, 156–157 . See also table view controller

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UIViewController class, 145–146 . See also view controller

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

unsigned integers, 247

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

unwrapping optionals, 61–64

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

user interface elements, 10

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

User Notifications framework. See notifications.

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Utilities pane, 10 , 124–125

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

V

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

value types, 115–116

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

variables, 18

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

changing value of, 20

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

declaring and creating, 19

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

naming, 22

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

var keyword, 19

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

vectors, 240

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

vertical pipe (| ), 248

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

view controller, 10 , 122

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

dismissing, 153–154

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

file, 124

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

in SpriteKit, 215

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

storyboard, 11

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

UIViewController class, 145

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

viewDidLoad() method, 145

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

used for setup, 149 , 162 , 215

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

viewWillAppear(_:) method, 146 , 181

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

void functions, 82

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

W

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

while loops, 50–53 , 228

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

X

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

.xcassets files, 207

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Xcode

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

autocomplete, 85

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

creating a new project, 6 , 122–125 , 205–206

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

installing, 4–5

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

opening, 5

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

playground, 17

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

signing in, 7

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

x-coordinates, 11 , 210

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Y

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

y-coordinates, 11 , 210

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

Z

zPosition property, of sprites, 220–221 , 225 , 270 , 283

zPosition property, of sprites, 220–221 , 225 , 270 , 283